#ifndef _NO_ACE_COMPONENT
#include "ace_TimeHandler.h"
#include <iostream>
namespace XGC
{
	namespace net
	{
		CTimeHandler::CTimeHandler(unsigned int Id, unsigned short Event)
		: m_Id(Id)
		, m_Event(Event)
		, m_ProactorTimer(-1)
		, m_Proactor(0)
		{
		}

		CTimeHandler::~CTimeHandler(void)
		{
			if(m_ProactorTimer != -1)
			{
				Close();
			}
		}

		void  CTimeHandler::handle_time_out(const ACE_Time_Value &tv, const void *act)
		{
			OnTimer(tv, act);
		}

		void CTimeHandler::Int2Tv(unsigned int time, ACE_Time_Value & tv)
		{
			unsigned int sec = time / 1000;
			unsigned int usec = time % 1000;
			tv.sec(sec);
			tv.usec(usec);
		}

		int CTimeHandler::Open(unsigned int startTime, unsigned int repeatTime, const void * arg, ACE_Proactor * proactor)
		{	
			m_Proactor = proactor;
			if(m_Proactor == 0)
			{
				m_Proactor = ACE_Proactor::instance();
			}
			
			ACE_Time_Value tv;
			ACE_Time_Value rtv;

			Int2Tv(startTime, tv);
			Int2Tv(repeatTime, rtv);

			m_ProactorTimer =  m_Proactor->schedule_timer(*this, arg, tv, rtv);
			return m_ProactorTimer;
		}

		void CTimeHandler::Close()
		{
			if(m_ProactorTimer != -1)
			{
				m_Proactor->cancel_timer(m_ProactorTimer);
				m_ProactorTimer = -1;
			}
		}

		void CTimeHandler::OnTimer(const ACE_Time_Value & tv, const void * arg)
		{
			std::cout << "Timeout" << std::endl;
		}
	}
}
#endif